package ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import utils.DateConverter;

import mainpackage.Category;
import mainpackage.DataManager;
import mainpackage.Todo;

public class MainPresenter {

	private boolean weekViewSelected = true;
	private MainWindow mainWindow;
	private DataManager dataManager;

	public MainPresenter(MainWindow mainWindow) {
		this.mainWindow = mainWindow;
	}

	public void setDataManager(DataManager dataManager) {
		this.dataManager = dataManager;
		fillCategoryList();
	}

	// *** For main window: ***

	public void addCategoryClicked() {
		if (dataManager.hasVacantColor()) {
			AddCategoryWindow window = new AddCategoryWindow(mainWindow);
			window.getPresenter().setDataManager(dataManager);
			window.getPresenter().setMainPresenter(this);
			window.setVisible(true);
		} else {
			JOptionPane.showMessageDialog(mainWindow,
					"Maximum number of categories reached.");
		}
	}

	// TODO temporary:
	public void addTodoClicked() {
		AddTodoWindow window = new AddTodoWindow(mainWindow);
		window.getPresenter().setDataManager(dataManager);
		// window.getPresenter().setMainPresenter(this);
		window.setVisible(true);

	}

	// TODO temporary:
	public void editTodoClicked() {
		Todo todo = dataManager.getTodoById(1);
		EditTodoWindow window = new EditTodoWindow(mainWindow);
		window.getPresenter().setDataManager(dataManager);
		window.getPresenter().setEditableTodo(todo);
		String start = DateConverter.dateToString(todo.getStartTime());
		String end = DateConverter.dateToString(todo.getEndTime());
		window.setData(todo.getName(), todo.getDescription(), start, end);
		// window.getPresenter().setMainPresenter(this);
		window.setVisible(true);

	}

	public void emptyWeekCalendarSpotClicked(/* TODO */) {
		// TODO
	}

	public void todoOnWeekCalendarClicked() {
		// TODO
	}

	public void dayOnMonthCalendarClicked() {
		// TODO
	}

	// TODO methods for ticking and unticking or one for both?
	public void categoryTicked(JCheckBox jCheckBox) {
		if (jCheckBox.isSelected()) {

		} else {

		}
		// TODO
	}

	public void weekViewButtonClicked() {
		// TODO
		if (!weekViewSelected) {
			mainWindow.setMonthButtonSelected(false);
			mainWindow.changeMainPanelContents("WEEK_PANEL");
			weekViewSelected = true;
		} else {
			mainWindow.setMonthButtonSelected(true);
			mainWindow.changeMainPanelContents("MONTH_PANEL");
			weekViewSelected = false;
		}
	}

	public void monthViewButtonClicked() {
		// TODO
		if (weekViewSelected) {
			mainWindow.setWeekButtonSelected(false);
			mainWindow.changeMainPanelContents("MONTH_PANEL");
			weekViewSelected = false;
		} else {
			mainWindow.setWeekButtonSelected(true);
			mainWindow.changeMainPanelContents("WEEK_PANEL");
			weekViewSelected = true;
		}
	}

	public void nextMonthOrWeekClicked() {
		// TODO
	}

	public void previousMonthOrWeekClicked() {
		// TODO
	}

	public void categoryDeletionClicked(JButton source) {
		String[] options = { "Yes", "No" };
		String message = "This will delete all associated todos. Proceed?";
		int answer = JOptionPane.showOptionDialog(mainWindow, message, "",
				JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null,
				null, options[1]);
		if (answer == 0) {
			JPanel panel = (JPanel) source.getParent();
			JCheckBox checkbox = (JCheckBox) panel.getComponent(1);
			try {
				deleteTodosOfCategory(checkbox.getText());
				dataManager.removeCategory(checkbox.getText());
				mainWindow.removeCheckBoxPanel(panel);
				mainWindow.revalidate();
				mainWindow.repaint();
			} catch (SQLException e) { //
				// gets here if foreign key constant violated
			}
		}

	}

	private void fillCategoryList() {
		List<Category> categories = dataManager.getCategories();
		for (Category c : categories) {
			mainWindow.addCheckBoxPanel(createCheckboxPanel(c));
		}
	}

	public void addCategoryToList(Category c) {
		mainWindow.addCheckBoxPanel(createCheckboxPanel(c));
		mainWindow.revalidate();
		mainWindow.repaint();
	}

	private JPanel createCheckboxPanel(Category c) {

		JPanel panel = new JPanel();
		panel.setBackground(Color.WHITE);
		// categoriesPanel.add(panel);
		panel.setLayout(new BorderLayout());

		JButton closeButton = new JButton("X");
		closeButton.setMargin(new Insets(2, 8, 2, 8));
		closeButton.setFont(new Font("Tahoma", Font.BOLD, 11));

		JCheckBox checkBox = new JCheckBox(c.getName());
		Color fontColor = convertMyColorToAwtColor(c.getColor());
		checkBox.setBackground(new Color(250, 250, 250));
		checkBox.setForeground(fontColor);
		checkBox.setFont(new Font("Tahoma", Font.BOLD, 11));
		checkBox.setAlignmentX(Component.CENTER_ALIGNMENT);
		checkBox.setEnabled(true);

		checkBox.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				categoryTicked((JCheckBox) (arg0.getSource()));
			}
		});

		closeButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				categoryDeletionClicked((JButton) arg0.getSource());

			}
		});

		panel.add(closeButton, BorderLayout.EAST);
		panel.add(checkBox, BorderLayout.CENTER);

		return panel;
	}

	private Color convertMyColorToAwtColor(mainpackage.Color myColor) {
		switch (myColor) {
		case RED:
			return Color.RED;
		case BLUE:
			return Color.BLUE;
		case CYAN:
			return new Color(0, 215, 215);
		case GREEN:
			return new Color(34, 177, 76);
		case PURPLE:
			return Color.MAGENTA;
		case YELLOW:
			return new Color(255, 175, 0);
		default:
			return Color.RED;
		}
	}

	private void deleteTodosOfCategory(String categoryName) {
		List<String> categoryNameList = new ArrayList<String>();
		categoryNameList.add(categoryName);
		List<Todo> toBeDeletedTodos = dataManager.getAllTodos(categoryNameList);
		for (Todo todo : toBeDeletedTodos) {
			dataManager.removeTodo(todo);
		}
	}

}
